#include "spi_flash.h"

int main(int argc, char **argv)
{
    int spi_fd;
    uint8_t rx_buf[512];
    uint8_t tx_buf[16];

    /*引脚复用初始化*/
    spi_remap0();

    /*设备初始化*/
    if(spi_init(&spi_fd) < 0)
    {
        perror("Failed to execute spi_init()");
        return -1;
    }

    /*读ID*/
    flash_readID(&spi_fd);

    /*擦除所有数据*/
    spi_flash_erase_chip(&spi_fd);

    /*SPI写数据*/
    spi_flash_write(&spi_fd, 0x1000, tx_buf, 16);

    /*SPI读数据*/
    spi_flash_read(&spi_fd, 0x1000, rx_buf, 512);
    for(int i = 0; i < strlen(rx_buf); ++i)
    {
        printf("%02X ", rx_buf[i]);
        if (!((i + 1) % 10))
            printf("\n");
    }
    printf("\n");

    /*释放SPI设备*/
    spi_uninit(&spi_fd);

    return 0;
}